//
//  MovieCollectHandle.m
//  Douban1.0
//
//  Created by dllo on 15/4/1.
//  Copyright (c) 2015年 www.lanoukeji.com蓝鸥科技. All rights reserved.
//

#import "NewsCollectHandle.h"


@implementation NewsCollectHandle
+ (instancetype)shareNewsHandle
{
    static NewsCollectHandle *newsCollectHandle = nil;
    if (newsCollectHandle == nil) {
        newsCollectHandle = [[NewsCollectHandle alloc]init];
        
    }
    return newsCollectHandle;
}

static sqlite3 *db = nil;

- (void)openMC
{
    if (db != nil) {
        NSLog(@"数据库已经打开了");
        return;
    }
    
    // 创建本地文件路径
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"array   %@", [array lastObject]);
    NSString *documentPath = [array lastObject];
    NSString *squlistFilePath = [documentPath stringByAppendingPathComponent:@"news.sqlite"];
    NSLog(@"%@",squlistFilePath);
    // 创建数据库
    // 将本地文件和数据库对象关联在一起
    BOOL result =  sqlite3_open(squlistFilePath.UTF8String, &db);
    
    if (result == SQLITE_OK) {
        NSLog( @"数据库打开成功");
        
    }else
    {
        NSLog(@"打开失败");
    }
}

- (void)createMC
{
    // 编写sql语句  number 是主键
    NSString *createSql = @"CREATE TABLE IF NOT EXISTS news(number INTEGER PRIMARY KEY AUTOINCREMENT, article_id TEXT, article_url TEXT, article_title TEXT, article_rzlx TEXT, article_date TEXT, article_format_date TEXT, article_pic TEXT, article_collection TEXT, article_comment TEXT, article_format_pic TEXT, article_favorite TEXT, article_filter_content TEXT)";
    // 执行sql语句
    BOOL result =  sqlite3_exec(db, createSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
    }
    
}

// 往表里插入数据
-(void)insertNews:(YBNewsDetailModel *)news
{
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO news(article_id,article_url,article_title,article_rzlx,article_date,article_format_date,article_pic,article_collection,article_comment,article_format_pic,article_favorite,article_filter_content) VALUES('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')", news.article_id, news.article_url, news.article_title, news.article_rzlx, news.article_date, news.article_format_date, news.article_pic, news.article_collection, news.article_comment, news.article_format_pic, news.article_favorite, nil];
     //执行
    NSLog(@"ceshi0000%@", insertSql);
    if (news.article_id.length == 0) {
        NSLog(@"+++++++++++++++++++kong");
    }else{
        NSLog(@"%@", news.article_id);
        int result = sqlite3_exec(db, insertSql.UTF8String, NULL, NULL, nil);
        if (result == SQLITE_OK) {
            NSLog(@"插入成功");
        }else{
            NSLog(@"插入失败%d", result);
        }
    }
}

- (NSMutableArray *)selectAllNews
{
    NSString *selectSql = @"SELECT * FROM news";
    // 创建数据库跟随指针
    sqlite3_stmt *stmt = nil;
    // 查询数据库之前准备工作
    // -1 代表不限制sql语句长度
    // 将数据库对象db、跟随指针stmt、sql语句关联起来进行循环查询工作
    NSInteger result =  sqlite3_prepare_v2(db, selectSql.UTF8String, -1, &stmt, nil );
    NSLog(@"result = %ld", result);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        
        NSMutableArray *array = [NSMutableArray array];
        // 开始循环查询每一条数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 取出某一行的第一列数据
            int number =  sqlite3_column_int(stmt, 0);

            const unsigned char *article_id = sqlite3_column_text(stmt, 1);
            const unsigned char *article_url = sqlite3_column_text(stmt, 2);
            const unsigned char *article_title = sqlite3_column_text(stmt, 3);
            const unsigned char *article_rzlx = sqlite3_column_text(stmt, 4);
            const unsigned char *article_date = sqlite3_column_text(stmt, 5);
            const unsigned char *article_format_date = sqlite3_column_text(stmt, 6);
            const unsigned char *article_pic = sqlite3_column_text(stmt, 7);
            const unsigned char *article_collection = sqlite3_column_text(stmt, 8);
            const unsigned char *article_comment = sqlite3_column_text(stmt, 9);
            const unsigned char *article_format_pic = sqlite3_column_text(stmt, 10);
            const unsigned char *article_favorite = sqlite3_column_text(stmt, 11);


            YBNewsDetailModel *news = [[YBNewsDetailModel alloc]init];
            news.article_id= [NSString stringWithUTF8String:(const char *)article_id];
            news.article_url= [NSString stringWithUTF8String:(const char *)article_url];
            news.article_title= [NSString stringWithUTF8String:(const char *)article_title];
            news.article_rzlx= [NSString stringWithUTF8String:(const char *)article_rzlx];
            news.article_date= [NSString stringWithUTF8String:(const char *)article_date];
            news.article_format_date= [NSString stringWithUTF8String:(const char *)article_format_date];
            news.article_pic= [NSString stringWithUTF8String:(const char *)article_pic];
            news.article_collection= [NSString stringWithUTF8String:(const char *)article_collection];
            news.article_comment= [NSString stringWithUTF8String:(const char *)article_comment];
            news.article_format_pic= [NSString stringWithUTF8String:(const char *)article_format_pic];
            news.article_favorite= [NSString stringWithUTF8String:(const char *)article_favorite];
            
            [array addObject:news];

            
        }
        return  array;
    }else{
        NSLog(@"查询失败");
    }
    
    
    
    return nil;
    
}

- (void)dropTable
{
    NSString *dropSql = @"DROP TABLE news";
    BOOL result = sqlite3_exec(db, dropSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
        NSLog(@"删除失败");
    }
    
    
}



@end
